package com.java.teachercode.copycode_2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

/**
 * @author luzhikun
 * @create 2021-02-01 19:30
 */
public class Miaobiao extends JFrame {
    JButton start, pause, stop;
    JLabel hour, min, sec;
    Start ss;

    public static void main(String[] args) {
        new Miaobiao();
    }

    public Miaobiao() {

        /*

        Jframe制作一个秒表界面

        */
        this.setTitle("秒表");
        this.setSize(500, 300);
        this.setLocationRelativeTo(null);
        Container c = this.getContentPane();
        c.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*

        时钟

        */
        hour = new JLabel("00");
        hour.setBounds(80, 70, 80, 50);
        hour.setFont(new Font("Consolas", Font.BOLD, 50));
        hour.setHorizontalAlignment(JLabel.CENTER);

        /*
         *时钟分钟件冒号
         *
         * */
        JLabel m_1 = new JLabel(":");
        m_1.setBounds(170, 70, 30, 50);
        m_1.setFont(new Font("Consolas", Font.BOLD, 50));

        /*

        分钟

        */
        min = new JLabel("00");
        min.setBounds(200, 70, 80, 50);
        min.setFont(new Font("Consolas", Font.BOLD, 50));
        min.setHorizontalAlignment(JLabel.CENTER);

        /*

        分钟和秒钟间的冒号

        */
        JLabel m_2 = new JLabel(":");
        m_2.setBounds(290, 70, 30, 50);
        m_2.setFont(new Font("Consolas", Font.BOLD, 50));

        /*

        秒钟
        *
        * */
        sec = new JLabel("00");
        sec.setBounds(320, 70, 80, 50);
        sec.setFont(new Font("Consolas", Font.BOLD, 50));
        sec.setHorizontalAlignment(JLabel.CENTER);


        /*

        开始按钮
        *
        * */
        start = new JButton("开始");
        start.setBounds(30, 200, 100, 30);
        start.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                stop.setEnabled(true);
                pause.setEnabled(true);
                ss = new Start(hour, min, sec);
                ss.start();
            }
        });


        /*

        暂停按钮
        *
        * */
        pause = new JButton("暂停");
        pause.setBounds(190, 200, 100, 30);
        pause.setEnabled(false);
        pause.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(true);
                stop.setEnabled(true);
                pause.setEnabled(false);
                new Pause(ss).start();
            }
        });


        /*


        结束按钮
        *
        * */
        stop = new JButton("结束");
        stop.setBounds(350, 200, 100, 30);
        stop.setEnabled(false);
        stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(true);
                stop.setEnabled(false);
                pause.setEnabled(false);
                new Pause(ss).start();
                sec.setText("00");
                min.setText("00");
                hour.setText("00");

            }
        });

        c.add(start);
        c.add(pause);
        c.add(stop);
        c.add(hour);
        c.add(m_1);
        c.add(min);
        c.add(m_2);
        c.add(sec);
        this.setVisible(true);

    }
}

class Start extends Thread {
    JLabel s, m, h;

    public Start(JLabel h, JLabel m, JLabel s) {
        this.h = h;
        this.m = m;
        this.s = s;
    }

    @Override
    public void run() {
        int time_s, time_m, time_h;
        time_s = Integer.parseInt(s.getText());
        time_m = Integer.parseInt(m.getText());
        time_h = Integer.parseInt(h.getText());
        //调整时间的输出格式
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumIntegerDigits(2);
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                //如果中断此线程，跳出死循环从而结束线程
                break;
            }
            time_s++;
            if (time_s >= 60) {
                time_m++;
                time_s = 0;
                //秒钟等于60，分钟加1，秒钟置0
                if (time_m >= 60) {
                    time_h++;
                    time_m = 0;
                    if (time_m >= 24) {
                        time_h = 0;
                    }
                }
            }

            s.setText(nf.format(time_s));
            //  格式化输出 e.g. 0:0:1——>00:00:01
            m.setText(nf.format(time_m));
            h.setText(nf.format(time_h));

        }
    }

}

class Pause extends Thread {
    Start ss;

    public Pause(Start ss) {
        this.ss = ss;
    }

    @Override
    public void run() {
        ss.interrupt();
    }

}
